FrameLib  2.0
DSP processing with frames of arbitrary timing and length
FrameLib_Context.h
Go to the documentation of this file.
1 
2 #ifndef FRAMELIB_CONTEXT_H
3 #define FRAMELIB_CONTEXT_H
4 
5 #include "FrameLib_Types.h"
6 #include "FrameLib_Global.h"
7 #include "FrameLib_Errors.h"
8 
22 {
23  using Global = FrameLib_Global;
24 
25  // Comparisions
26 
27  friend bool operator == (const FrameLib_Context& a, const FrameLib_Context& b)
28  {
29  return a.mGlobal == b.mGlobal && a.mReference == b.mReference;
30  }
31 
32  friend bool operator != (const FrameLib_Context& a, const FrameLib_Context& b)
33  {
34  return !(a == b);
35  }
36 
47  template <class T, T *(Global::*getMethod)(void *), void(Global::*releaseMethod)(void *)>
48  class ManagedPointer
49  {
50 
51  public:
52 
53  // Constructor / Destructor
54 
55  ManagedPointer(const FrameLib_Context &context) : mGlobal(context.mGlobal), mReference(context.mReference)
56  {
57  mPointer = (mGlobal->*getMethod)(mReference);
58  }
59 
60  ~ManagedPointer()
61  {
62  release();
63  }
64 
65  // Non-copyable
66 
67  ManagedPointer(const ManagedPointer&) = delete;
68  ManagedPointer& operator=(const ManagedPointer&) = delete;
69 
70  // Release
71 
72  void release()
73  {
74  if (mGlobal)
75  (mGlobal->*releaseMethod)(mReference);
76  mPointer = nullptr;
77  mGlobal = nullptr;
78  mReference = nullptr;
79  }
80 
81  // Pointer / Bool Conversion
82 
83  T *operator->() { return mPointer; }
84  operator bool() const { return mPointer != nullptr; }
85 
86  private:
87 
88  // Member Variables
89 
90  T *mPointer;
91  FrameLib_Global *mGlobal;
92  void *mReference;
93  };
94 
95 public:
96 
97  // Constructor - the reference should be a suitable reference address in the host environment
98 
99  FrameLib_Context(FrameLib_Global *global, void *reference) : mGlobal(global), mReference(reference) {}
100 
101  // Construct one of these objects to retain a relevant object
102 
103  using Allocator = ManagedPointer<FrameLib_LocalAllocator, &Global::getAllocator, &Global::releaseAllocator>;
104  using ProcessingQueue = ManagedPointer<FrameLib_ProcessingQueue, &Global::getProcessingQueue, &Global::releaseProcessingQueue>;
105 
106  // Get the global as a FrameLib_ErrorReporter from the context
107 
108  operator FrameLib_ErrorReporter&() { return *mGlobal; }
109 
110 private:
111 
112  // Member Variables
113 
114  FrameLib_Global *mGlobal;
115  void *mReference;
116 };
117 
118 
119 #endif
FrameLib_Context(FrameLib_Global *global, void *reference)
Definition: FrameLib_Context.h:99
a class for containing and managing FrameLib&#39;s global resources.
Definition: FrameLib_Global.h:28
a class used to represent distinct non-connectable areas in the host environment. ...
Definition: FrameLib_Context.h:21
ManagedPointer< FrameLib_LocalAllocator, &Global::getAllocator, &Global::releaseAllocator > Allocator
Definition: FrameLib_Context.h:103
a class used to report errors to the host environment.
Definition: FrameLib_Errors.h:34
friend bool operator==(const FrameLib_Context &a, const FrameLib_Context &b)
Definition: FrameLib_Context.h:27
friend bool operator!=(const FrameLib_Context &a, const FrameLib_Context &b)
Definition: FrameLib_Context.h:32
ManagedPointer< FrameLib_ProcessingQueue, &Global::getProcessingQueue, &Global::releaseProcessingQueue > ProcessingQueue
Definition: FrameLib_Context.h:104